/*
 * Project: Flying Lantern
 * File: CGame.go
 * Desc: Holds the CGame object that handles game logic.
 * License: MIT
 */

package fl

import (
	"github.com/0xe2-0x9a-0x9b/Go-SDL/sdl"
	"time"
)

type CGame struct {
	mRunning     bool
	mLastTick    time.Time
	mCurrentTick time.Time
	mTimeDelta   time.Duration
}

// Set whether or not the game is running
func (g *CGame) SetRunning(isRunning bool) {
	g.mRunning = isRunning
}

// Get whether or not the game is running
func (g *CGame) IsRunning() bool {
	return g.mRunning
}

// Get our time delta
func (g *CGame) GetTimeDelta() time.Duration {
	return g.mTimeDelta
}

// Initialize our game object
func (g *CGame) Init() {
	g.mRunning = true

	// Init times
	g.mLastTick = time.Now()
	g.mCurrentTick = time.Now()
	g.mTimeDelta = g.mCurrentTick.Sub(g.mLastTick)
}

// Update our game logic
func (g *CGame) Update() {

	// Timing
	g.mLastTick = g.mCurrentTick
	g.mCurrentTick = time.Now()
	g.mTimeDelta = g.mCurrentTick.Sub(g.mLastTick)

	// Handle Events
	g.HandleEvents()
}

// Handle Events ( SDL , Keys , etc. )
func (g *CGame) HandleEvents() {
	select {

	// Catch our SDL Events
	case _event := <-sdl.Events:
		switch _event.(type) {
		// Did we close the window? Stop running.
		case sdl.QuitEvent:
			g.SetRunning(false)
		default:
			break
		}

	default:
		break
	}
}
